﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.Core.Extension;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL.Hotel
{
    //菜谱分类表 
    public partial class H_Hotel_CookbookCategory
	{
   		     
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from H_Hotel_CookbookCategory");
			strSql.Append(" where ");
			                                       strSql.Append(" Id = @Id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        public bool ExistsHotelId(int hotelId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from H_Hotel_CookbookCategory");
            strSql.Append(" where ");
            strSql.Append(" HotelId = @HotelId  ");
            SqlParameter[] parameters = {
					new SqlParameter("@HotelId", SqlDbType.Int,4)
			};
            parameters[0].Value = hotelId; 
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        } 	
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.Hotel.H_Hotel_CookbookCategory model)
		{
            var strSql = new StringBuilder();
            strSql.Append("insert into H_Hotel_CookbookCategory(");
            strSql.Append("Name,Code,Parentid,HotelId,RoleId,IsDisplay");
            strSql.Append(") values (");
            strSql.Append("@Name,@Code,@Parentid,@HotelId,@RoleId,@IsDisplay");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Code", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Parentid", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@RoleId", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@IsDisplay", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.Name;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Parentid;
            parameters[3].Value = model.HotelId;
            parameters[4].Value = model.RoleId;
            parameters[5].Value = model.IsDisplay;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }					   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.Hotel.H_Hotel_CookbookCategory model)
		{
            var strSql = new StringBuilder();
            strSql.Append("update H_Hotel_CookbookCategory set ");

            strSql.Append(" Name = @Name , ");
            strSql.Append(" Code = @Code , ");
            strSql.Append(" Parentid = @Parentid , ");
            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" RoleId = @RoleId , ");
            strSql.Append(" IsDisplay = @IsDisplay  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Code", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Parentid", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@RoleId", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@IsDisplay", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Parentid;
            parameters[4].Value = model.HotelId;
            parameters[5].Value = model.RoleId;
            parameters[6].Value = model.IsDisplay;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}


        public bool ExistsCoookBook(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Count(1) from H_Hotel_Cookbook where CategoryId=@Id"); 
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id; 
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
  

			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from H_Hotel_CookbookCategory ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id; 
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from H_Hotel_CookbookCategory ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.Hotel.H_Hotel_CookbookCategory GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select Id, Name, Code, Parentid, HotelId, RoleId, IsDisplay  ");				
			strSql.Append("  from H_Hotel_CookbookCategory ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id; 

            var  model = new RBS.Core.Model.Hotel.H_Hotel_CookbookCategory();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model = ds.Tables[0].Rows[0].ConvertToEntity<RBS.Core.Model.Hotel.H_Hotel_CookbookCategory>();
                return model;
            }
            else
            {
                return null;
            }
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM H_Hotel_CookbookCategory ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM H_Hotel_CookbookCategory ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		 #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM H_Hotel_CookbookCategory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod

   
	}
}

